from django.db import models
from django.utils.timezone import now


# Create your models here.
from user.models import UserProfile


class GoodsCategory(models.Model):
    """
    商品分类
    """
    CATEGORY_TYPE = (
        (1,"一级类目"),
        (2,"二级目录"),
    )

    name = models.CharField('类别名',default="",max_length=30,help_text="类别名")
    code = models.CharField("类别编码",default="",max_length=30,help_text="类别编码")
    desc = models.CharField("类别描述",default="",max_length=200)
    image = models.ImageField(upload_to="goodscategory",blank=True, verbose_name="分类图标")
    category_type = models.IntegerField("类目级别",choices=CATEGORY_TYPE,help_text="类目级别")
    parent_category = models.ForeignKey("self", on_delete=models.CASCADE, null=True, blank=True, verbose_name="父类目级别",
                                        help_text="父目录",
                                        related_name="sub_cat")
    is_tab = models.BooleanField("是否导航", default=False, help_text="是否导航")
    add_time = models.DateTimeField("添加时间", default=now)

    class Meta:
        verbose_name = "商品类别"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Goods(models.Model):
    """
    商品
    """
    category = models.ForeignKey(GoodsCategory,on_delete=models.DO_NOTHING,verbose_name="商品类目",null=True)
    goods_sn = models.CharField("商品唯一货号",max_length=50,default="")
    #name = models.CharField("商品名称",max_length=100)
    click_num = models.IntegerField("点击数",default=0)
    goods_price = models.CharField("价格",max_length=100)
    goods_brief = models.CharField("商品简介",max_length=300)
    ship_free = models.BooleanField("是否承担运费",default=True)
    goods_front_image = models.ImageField(upload_to="goodsfront",blank=True, verbose_name="封面图")
    is_new = models.BooleanField("是否全新", default=False)
    add_time = models.DateTimeField("添加时间", default=now)
    seller = models.ForeignKey(UserProfile, on_delete=models.DO_NOTHING,verbose_name="卖家",related_name='seller',to_field='username')#卖家
    buyer = models.ForeignKey(UserProfile, on_delete=models.DO_NOTHING,null=True,verbose_name="买家",related_name='buyer',to_field='username')#买家

    class Meta:
        verbose_name = "商品信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods_brief

class GoodsImage(models.Model):
    """
    商品图片
    """
    good = models.ForeignKey(Goods,on_delete=models.CASCADE,verbose_name="所属商品",related_name='images')
    image = models.ImageField(upload_to="",verbose_name="图片",blank=True)#ImageField继承自FileField,默认最大长度是100字符，可以设置max_length
    add_time = models.DateTimeField("添加时间", default=now)

    class Meta:
        verbose_name = "商品图片"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.good.name

class GoodsNavImage(models.Model):
    """
    商品分类封面
    """
    goodscategory = models.OneToOneField(GoodsCategory,on_delete=models.CASCADE,related_name='goodscategoryimage')
    image = models.ImageField(upload_to='',verbose_name='分类图片',blank=True)
    add_time = models.DateTimeField("添加时间",default=now)

    class Meta:
        verbose_name = "分类图片"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goodscategory.name
